<?php
		
		if(isset($_POST['ajax_pagination'])){
					$id=$_GET['id'];
					$cache=new Cache;
					$_GET['page']=$_POST['page'];
		$db=new Model2;
		$id=mysql_real_escape_string($id);
		//-------------------------- GET TRUYEN ----------------
		$db->query("select * from truyen_theloai where alias='".$id."'");
					if($db->num_rows()>0){
						$r1=$db->fetchOne();
						$db->close();
					
					$data['theloai']=$r1;
					$url=truyenf1_url_truyen_theloai($r1['id'],$r1['alias']).'/';
					$og_url=truyenf1_url_truyen_theloai($r1['id'],$r1['alias']);
					$where="";
					$order="";
					if($_POST['ext']!=''){
					$_GET['ext']=$_POST['ext'];
					}
					if(isset($_GET['ext'])){
						$url=truyenf1_url_truyen_theloai($r1['id'],$r1['alias']).'/'.$_GET['ext'].'/';
						$og_url=truyenf1_url_truyen_theloai($r1['id'],$r1['alias']).'/'.$_GET['ext'];
						 switch($_GET['ext']){
							case 'date': $order=' order by time_upload desc'; break;
							case 'views': $order=' order by views desc'; break;
							case 'sochuong': $order=' order by tongsochuong desc';  break;
							case 'hoanthanh': $where=' and tinhtrang=1';  break;
							case 'pick': $where=' and pick=1';  break;
							case 'views': $order=' order by views desc';  break;
							case 'view_day': $order=' order by view_day desc';  break;
							case 'view_week': $order=' order by view_week desc';  break;
							case 'view_month': $order=' order by view_month desc';  break;
						 }
					}
					
					
						
						if($where){
							$sql ="select * from truyen_theloai_fk where theloai_id=".$r1['id']." and duyet=1 ".$where." order by truyen_id desc";
							$sql2="select truyen_id from truyen_theloai_fk where theloai_id=".$r1['id']." and duyet=1".$where;
							}else
							if($order){
							$sql ="select * from truyen_theloai_fk where theloai_id=".$r1['id']." and duyet=1 ".$order;
							$sql2="select truyen_id from truyen_theloai_fk where theloai_id=".$r1['id']." and duyet=1";
							}else{
							$sql ="select * from truyen_theloai_fk where theloai_id=".$r1['id']." and duyet=1 order by truyen_id desc";
							$sql2="select truyen_id from truyen_theloai_fk where theloai_id=".$r1['id']." and duyet =1";
							}
					
					//echo $sql2;
					
					$pager = new Pagination2($sql,24,4,$url,1800,'truyen_2');
					
					$pager->ajax=1;
					$pager->setDebug(false);
					$data['truyenmoi']['data'] = $pager->paginate($cache,$sql2);
					$data['truyenmoi']['nav']=$pager->renderFullNav();
					//Render data---?>
                      <div style="text-align:center"><?php echo $data['truyenmoi']['nav']; ?></div>
					<?php              	echo truyen_render_list_item($data['truyenmoi']['data']); ?>
       				  <div style="text-align:center"><?php echo $data['truyenmoi']['nav']; ?></div> 
					
					 <?php
				
					}
						exit();
		}
		
		direct_url();
		$id=$_GET['id']; //string nha
		
		$db=new Model2;
		$id=mysql_real_escape_string($id);
		//-------------------------- GET TRUYEN ----------------
		$db->query("select * from truyen_theloai where alias='".$id."'");
		if($db->num_rows()>0){
				$r1=$db->fetchOne();
				$db->close();
				$data['theloai']=$r1;

				$cache=new Cache;
				//TRUYEN PICK
				$sql ="select truyen_id from truyen_theloai_fk  where theloai_id=".$r1['id']." and pick = 1 and duyet=1 order by time_update desc ";
				$sql2="select truyen_id from truyen_theloai_fk where theloai_id=".$r1['id']." and pick = 1 and duyet=1 ";
			
				$pager = new Pagination2($sql,6,4,'',0,"truyen_2");
				$pager->setDebug(false);
				$data['decu'] = $pager->paginate($cache,$sql2);
				$total= $pager->total_rows;
				$data['decu_page'] = ceil($total / 6);
				
		
		
				//TRUYEN MOI
				$sql ="select truyen_id from truyen_theloai_fk  where  theloai_id=".$r1['id']." and duyet=1 order by time_upload desc ";
				$sql2="select truyen_id from truyen_theloai_fk where theloai_id=".$r1['id'].' and duyet=1';
			
				$pager = new Pagination2($sql,6,4,'',0,"truyen_2");
				$pager->setDebug(false);
				$data['moi'] = $pager->paginate($cache,$sql2);
				$total= $pager->total_rows;

				$data['moi_page'] = ceil($total / 6);
			
		
		
		
		$url=truyenf1_url_truyen_theloai($r1['id'],$r1['alias']).'/';
		$og_url=truyenf1_url_truyen_theloai($r1['id'],$r1['alias']);
		$where="";
		$order="";
		$title="";
		if(isset($_GET['ext'])){
			$url=truyenf1_url_truyen_theloai($r1['id'],$r1['alias']).'/'.$_GET['ext'].'/';
			$og_url=truyenf1_url_truyen_theloai($r1['id'],$r1['alias']).'/'.$_GET['ext'];
			 switch($_GET['ext']){
			 	case 'date': $order=' order by time_upload desc'; $title=' Mới Đăng '; break;
				case 'sochuong': $order=' order by tongsochuong desc'; $title=' Nhiều Chương '; break;
				case 'hoanthanh': $where=' and tinhtrang=1'; $title=' Full '; break;
				case 'pick': $where=' and pick=1'; $title=' Đề Cử - Hay Nhất - Hot Nhất ';  break;
				case 'views': $order=' order by views desc';$title='Xem nhiều nhất, Hot Nhất, Hay nhất ';  break;
				case 'view_day': $order=' order by view_day desc'; $title='Xem nhiều, hot nhất trong ngày '; break;
				case 'view_week': $order=' order by view_week desc';$title='Xem nhiều, hot nhất trong tuần ';  break;
				case 'view_month': $order=' order by view_month desc'; $title='Xem nhiều , hot nhất trong tháng '; break;
			 }
		}
		
		
		if($where){
		$sql ="select * from truyen_theloai_fk where theloai_id=".$r1['id']." and duyet=1 ".$where." order by truyen_id desc";
		$sql2="select truyen_id from truyen_theloai_fk where theloai_id=".$r1['id']." and duyet=1".$where;
		}else
		if($order){
		$sql ="select * from truyen_theloai_fk where theloai_id=".$r1['id']." and duyet=1 ".$order;
		$sql2="select truyen_id from truyen_theloai_fk where theloai_id=".$r1['id']." and duyet=1";
		}else{
		$sql ="select * from truyen_theloai_fk where theloai_id=".$r1['id']." and duyet=1 order by truyen_id desc";
		$sql2="select truyen_id from truyen_theloai_fk where theloai_id=".$r1['id']." and duyet =1";
		}
		
		//echo $sql2;
		
		$pager = new Pagination2($sql,24,4,$url,1800,'truyen_2');
		$pager->ajax=1;
		$pager->setDebug(false);
		$data['info'] = $pager->paginate($cache,$sql2);
		$data['nav']	= $pager->renderFullNav();
		}
	
	
			$data1['og']='<link rel="canonical" href="'.$og_url.'" /><meta name="ROBOTS" content="INDEX, FOLLOW"/>';
			$data1[title]=web_title($r1['seo_title'].$title);
			$data1['mota']=$r1['seo_description'].$title;
			$data1['keyword']=$r1['seo_keyword'].",".$title;
			
			loadview('template/header_truyen',$data1);
			loadview("truyen/list_theloai",$data);
			loadview('template/footer_truyen');
			exit();
			
			
